package com.lucky.ebidding.service;

import com.lucky.ebidding.dao.ExpertDao;
import com.lucky.ebidding.dao.UserDao;
import com.lucky.ebidding.dao.UserShInfoDao;
import com.lucky.ebidding.dto.UserAndUserSh;
import com.lucky.ebidding.model.*;
import com.lucky.ebidding.service.interfaces.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * ClassName:UserService
 * Package:com.lucky.ebidding.service
 * Description:
 *
 * @Date:2020/3/14 21:33
 * @Author:857251389@qq.com
 */
@Service
public class UserService implements UserServiceI {

    @Autowired
    private UserDao userDao;

    @Autowired
    private ExpertDao expertDao;

    @Autowired
    private UserShInfoDao userShInfoDao;

    @Autowired
    private NotificationService notificationService;

    @Override
    public User login(String account, String password) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andAccountEqualTo(account);
        List<User> users = userDao.selectByExample(userExample);
        if (users.size() != 0) {
            User user = users.get(0);
            if (user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public boolean register(User user) {

        int insert = userDao.insert(user);
        if(user.getStatus().equals(2)){
            Expert expert = new Expert();
            expert.setUserId(user.getId());
            expert.setName(user.getName());
            expert.setExpertId(UUID.randomUUID().toString().substring(0,8)+LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
            expertDao.insertSelective(expert);
        }
        if(user.getStatus().equals(0)){
            UserShInfo userShInfo = new UserShInfo();
            userShInfo.setUserId(user.getId());
            userShInfo.setUserType(0);
            userShInfoDao.insertSelective(userShInfo);
        }
        if(user.getStatus().equals(1)){
            UserShInfo userShInfo = new UserShInfo();
            userShInfo.setUserId(user.getId());
            userShInfo.setUserType(1);
            userShInfoDao.insertSelective(userShInfo);
        }
        if(insert!=0){
            return true;
        }
        return false;
    }

    @Override
    public void updateUserShInfo(UserShInfo userShInfo) {
        userShInfoDao.updateByPrimaryKeySelective(userShInfo);
    }

    @Override
    public UserShInfo selectUserShInfoByUserId(Integer id) {
        UserShInfoExample userShInfoExample = new UserShInfoExample();
        userShInfoExample.createCriteria().andUserIdEqualTo(id);

        List<UserShInfo> userShInfos = userShInfoDao.selectByExample(userShInfoExample);

        return userShInfos.get(0);
    }

    @Override
    public List<UserAndUserSh> selectAllZbUser(Integer status) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andStatusEqualTo(status);
        List<User> users = userDao.selectByExample(userExample);
        List<UserAndUserSh> userAndUserShes = new ArrayList<>();
        for (User user : users) {
            UserShInfoExample userShInfoExample = new UserShInfoExample();
            userShInfoExample.createCriteria().andUserIdEqualTo(user.getId());
            List<UserShInfo> userShInfos = userShInfoDao.selectByExample(userShInfoExample);
            UserAndUserSh userAndUserSh = new UserAndUserSh();
            userAndUserSh.setUser(user);
            userAndUserSh.setUserShInfo(userShInfos.get(0));
            userAndUserShes.add(userAndUserSh);
        }
        return userAndUserShes;
    }

    @Override
    public List<User> selectAllUser() {

        List<User> users = userDao.selectByExample(new UserExample());
        return users;
    }

    @Override
    public int shStatus(User user) {

        UserShInfoExample userShInfoExample = new UserShInfoExample();
        userShInfoExample.createCriteria().andUserIdEqualTo(user.getId());

        List<UserShInfo> userShInfos = userShInfoDao.selectByExample(userShInfoExample);
        if(userShInfos.get(0).getFileUploadFlag() ==0){
            //资格文件未上传
            return 3;
        }
        if(userShInfos.get(0).getShFlag() == 0){
            //待审核
            return 0;
        }

        if(userShInfos.get(0).getShFlag() == 2 ){
            //审核未通过
            return 2;
        }
        return 1;
    }

    @Override
    public int setStatus(Integer status, Integer id,User user) {
        UserShInfo userShInfo = new UserShInfo();
        userShInfo.setId(id);
        userShInfo.setShFlag(status);

        int i = userShInfoDao.updateByPrimaryKeySelective(userShInfo);
        UserShInfo userShInfo1 = userShInfoDao.selectByPrimaryKey(id);
        if(status == 1){
            notificationService.sendShNotification(userShInfo1.getUserId(),user.getId(),3,"通过");
        }else if(status == 2){
            notificationService.sendShNotification(userShInfo1.getUserId(),user.getId(),3,"未通过，请重新上传审核文件");
        }
        return i;
    }
}
